package com.zengdada.authority.controller;

import com.github.pagehelper.PageInfo;
import com.zengdada.authority.exception.RoutineException;
import com.zengdada.authority.model.DynamicTable;
import com.zengdada.authority.model.DynamicTableColumn;
import com.zengdada.authority.model.DynamicTableColumnSearch;
import com.zengdada.authority.service.DynamicTableAuthorityService;
import com.zengdada.authority.service.DynamicTableColumnAuthorityService;
import com.zengdada.authority.service.DynamicTableToolbarService;
import com.zengdada.authority.utils.JsonMessage;
import com.zengdada.authority.utils.mapper.Page;
import com.zengdada.authority.utils.mapper.Sort;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.zengdada.authority.utils.JsonMessage.fail;
import static com.zengdada.authority.utils.JsonMessage.success;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2019/3/9 14:16
 * @Description:
 */
@Controller
@RequestMapping("/authority/dynamic_table_column")
public class DynamicTableColumnAuthorityController {
    @Autowired
    @Qualifier("dynamicTableColumnAuthorityServiceImpl")
    DynamicTableColumnAuthorityService dynamicTableColumnAuthorityService;
    @Autowired
    @Qualifier("dynamicTableAuthorityServiceImpl")
    DynamicTableAuthorityService dynamicTableAuthorityService;
    @Autowired
    @Qualifier("dynamicTableToolbarServiceImpl")
    DynamicTableToolbarService dynamicTableToolbarService;

    @GetMapping(value = "/list.html")
    public String listPage(HttpServletRequest request, @RequestParam("dynamicTableId") String dynamicTableId) {
        request.setAttribute("title", "动态表格表头 管理");
        if (StringUtils.isBlank(dynamicTableId)) throw new RoutineException("异常请求，缺少dynamicTableId参数");
        request.setAttribute("dynamicTableId", dynamicTableId);
        return "authority/dynamic/table_column/list";
    }

    @PostMapping(value = "/list.do")
    @ResponseBody
    public JsonMessage list(@RequestParam(value = "searchs", required = false) String searchs, Page page, Sort sort) {
        try {
            PageInfo<Map<String, Object>> pageInfo = dynamicTableColumnAuthorityService.selectDetailedPageInfoByParam(searchs, page, sort);
            return success(pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @GetMapping(value = "/edit.html")
    public String edit(HttpServletRequest request, String id, @RequestParam("dynamicTableId") String dynamicTableId) {
        if (StringUtils.isBlank(dynamicTableId)) throw new RoutineException("异常请求，缺少dynamicTableId参数");
        DynamicTable dynamicTable = dynamicTableAuthorityService.selectById(dynamicTableId);
        if (dynamicTable == null)
            throw new RoutineException("异常请求，没有找到 dynamicTableId 为 " + dynamicTableId + " 的数据");
        request.setAttribute("dmlId", dynamicTable.getDmlId());
        request.setAttribute("id", id);
        request.setAttribute("dynamicTableId", dynamicTableId);
        request.setAttribute("title", StringUtils.isBlank(id) ? "动态表格表头 新增" : "动态表格表头 编辑");
        return "authority/dynamic/table_column/edit";
    }


    @InitBinder("search")
    public void initSearchBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("search.");
    }


    @PostMapping(value = "/save.do")
    @ResponseBody
    public JsonMessage save(@ModelAttribute("column") DynamicTableColumn dynamicTableColumn, @ModelAttribute("search") DynamicTableColumnSearch search) {
        try {
            dynamicTableColumnAuthorityService.save(dynamicTableColumn, search);
            return success();
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @RequestMapping(value = "/regenerate.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage regenerate(@RequestParam(value = "dynamicTableId", defaultValue = "") String dynamicTableId) {
        try {
            if ("".equals(dynamicTableId)) throw new Exception("请求参数错误!");
            dynamicTableColumnAuthorityService.autoGenerationColumn(dynamicTableId, false);
            return success("重新生成列 成功", null);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @PostMapping(value = "/detail/{id}.do")
    @ResponseBody
    public JsonMessage detail(@PathVariable(value = "id") String id) {
        try {
            return success(dynamicTableColumnAuthorityService.selectDetailedAndSearchById(id));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @PostMapping(value = "/table_toolbars.do")
    @ResponseBody
    public JsonMessage table_toolbars(@RequestParam(value = "tableId") String tableId) {
        try {
            return success(dynamicTableToolbarService.selectByTableId(tableId));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @PostMapping(value = "/del.do")
    @ResponseBody
    public JsonMessage del(@RequestParam(value = "id") String id) {
        try {
            return success("删除了" + dynamicTableColumnAuthorityService.batchDelete(id) + "条数据", null);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

}
